package sudoku;

import java.awt.Font;
import java.util.*;

class saves{
    int x;
    int y;
    int n;
    saves(int a, int b, int num){
        x=a;
        y=b;
        n=num;
    }
}
/**
 *
 * @author Rene
 */
public class Main {
    
    static int free_fields = 81;
    static Feld[][] feld = new Feld[9][9];
    static int free_fields_save = 81;
    static ArrayList<saves> feld_save = new ArrayList();
    
    static void start(){
        free_fields = 81;
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                feld[i][j] = new Feld(i,j);
                feld[i][j].setFont(new Font("sansserif", Font.PLAIN, 16));
            }
        }
    }
    
    static void repaintall(){
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                feld[i][j].repainttext();
            }
        }
    }
    
    static boolean solve1(){
        
        //nur eine Möglichkeit in einem Feld
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                if (feld[i][j].numbers.size()==1){
                    feld[i][j].enterNumber(feld[i][j].numbers.get(0));
                    return true;
                }
            }
        }
        
        //senkrecht, waagerecht
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                for (String s : feld[i][j].numbers){
                    int findings=0;
                    for (int x=0; x<9; x++){
                        if (feld[i][x].numbers.contains(s)) findings++;
                    }
                    if (findings == 1) {
                        feld[i][j].enterNumber(s);
                        return true;
                    }
                    findings = 0;
                    for (int x=0; x<9; x++){
                        if (feld[x][j].numbers.contains(s)) findings++;
                    }
                    if (findings == 1) {
                        feld[i][j].enterNumber(s);
                        return true;
                    }
                    int x1=0,x2=0,y1=0,y2=0;
                    if (i%3==0){
                        x1=1;
                        x2=2;
                    }
                    if (i%3==1){
                        x1=-1;
                        x2=1;
                    }
                    if (i%3==2){
                        x1=-2;
                        x2=-1;
                    }
                    if (j%3==0){
                        y1=1;
                        y2=2;
                    }
                    if (j%3==1){
                        y1=-1;
                        y2=1;
                    }
                    if (j%3==2){
                        y1=-2;
                        y2=-1;
                    }
                    findings = 0;
                    if (feld[i][j+y1].numbers.contains(s)) findings++;
                    if (feld[i][j+y2].numbers.contains(s)) findings++;
                    if (feld[i+x1][j].numbers.contains(s)) findings++;
                    if (feld[i+x1][j+y1].numbers.contains(s)) findings++;
                    if (feld[i+x1][j+y2].numbers.contains(s)) findings++;
                    if (feld[i+x2][j].numbers.contains(s)) findings++;
                    if (feld[i+x2][j+y1].numbers.contains(s)) findings++;
                    if (feld[i+x2][j+y2].numbers.contains(s)) findings++;
                    if (findings == 0) {
                        feld[i][j].enterNumber(s);
                        return true;
                    }
                }
            }
        }        
        
        // bei 2 Möglichkeiten raten
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                if (feld[i][j].numbers.size()==2){
                    String num = feld[i][j].numbers.get(0);
                    feld[i][j].enterNumber(num);
                    System.out.println("noch " + Main.free_fields + " Felder frei\ngeraten:" + 
                            num + " auf " + i + ":"+ j );
                    return true;
                }
            }
        }
        
        // sonst bei 3 Möglichkeiten raten
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                if (feld[i][j].numbers.size()==3){
                    String num = feld[i][j].numbers.get(0);
                    feld[i][j].enterNumber(num);
                    System.out.println("noch " + Main.free_fields + " Felder frei\ngeraten:" + 
                            num + " auf " + i + ":"+ j );
                    return true;
                }
            }
        }
        System.out.println("Fehler: zu wenig Daten");
        return false;
    }
    
    static void recalculate(int line_x, int line_y, String num){
        for(int i=0; i<9; i++){
            feld[line_x][i].delete(num);
            feld[i][line_y].delete(num);
        }
        int x1=0,x2=0,y1=0,y2=0;
        if (line_x%3==0){
            x1=1;
            x2=2;
        }
        if (line_x%3==1){
            x1=-1;
            x2=1;
        }
        if (line_x%3==2){
            x1=-2;
            x2=-1;
        }
        if (line_y%3==0){
            y1=1;
            y2=2;
        }
        if (line_y%3==1){
            y1=-1;
            y2=1;
        }
        if (line_y%3==2){
            y1=-2;
            y2=-1;
        }
        
        feld[line_x+x1][line_y+y1].delete(String.valueOf(num));
        feld[line_x+x1][line_y+y2].delete(String.valueOf(num));
        feld[line_x+x2][line_y+y1].delete(String.valueOf(num));
        feld[line_x+x2][line_y+y2].delete(String.valueOf(num));
    }
    
    public static void save(){ 
        feld_save.clear();
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                if (feld[i][j].entry!=0)feld_save.add(new saves(i,j,feld[i][j].entry));
            }
        }
    }
    
    public static void load(){
        System.out.println("Felder werden geleert!\nAlle vorherigen Einträge werden wiederhergestellt");
        free_fields = 81;
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                feld[i][j].reset();
            }
        }
        for (saves s : feld_save){
            feld[s.x][s.y].enterNumber(String.valueOf(s.n));
        }
        repaintall();
        feld_save.clear();
        System.out.println("Wiederherstellung komplett!");
    }
    
    public static void main(String[] args) {
        start();
        repaintall();
        gui.main();
    }
}
